package teacher

import (
	"anban/controllers"
	"anban/logic"
	"anban/service"
	"anban/utils"
	"fmt"
	"github.com/astaxie/beego"
	"html/template"
	"strconv"
)

type HomeworkNoticeController struct {
	controllers.BaseController
}

func (c *HomeworkNoticeController) ShowList() {
	c.Data["title"] = "作业历史"
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "teacher/homework-history.html"
}

func (c *HomeworkNoticeController) LoadMore() {
	count, _ := beego.AppConfig.Int("loadcount")
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}
	p := map[string]interface{}{}
	p["relation"] = true
	p["convert"] = true
	p["userId"] = c.GetSession("userId")
	p["curPage"] = curPage
	p["perCount"] = count
	_, c.Data["homeworkList"] = service.GetHomeworkList(p)
	c.TplName = "teacher/homework-history-list.html"
}

func (c *HomeworkNoticeController) DoAdd() {
	var res *utils.ResJsonStruct
	input := c.Input()

	userId := c.GetSession("userId")
	input["user_id"] = []string{fmt.Sprintf("%d", userId)}

	 err := logic.AddHomework(input)
	if err != nil {
		res = utils.ResJson(err.Code(), err.Error(), "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	res = utils.ResJson(1, "作业发布成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}
